package com.tudouji.project.store.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.project.store.domain.Storehouseroom;
import com.tudouji.project.store.service.IStorehouseroomService;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.web.page.TableDataInfo;

/**
 * 库间管理Controller
 * 
 * @author wf
 * @date 2021-01-04
 */
@RestController
@RequestMapping("/store/storehouseroom")
public class StorehouseroomController extends BaseController
{
    @Autowired
    private IStorehouseroomService storehouseroomService;

    /**
     * 查询库间管理列表
     */
    @PreAuthorize("@ss.hasPermi('store:storehouseroom:list')")
    @GetMapping("/list")
    public TableDataInfo list(Storehouseroom storehouseroom)
    {
        startPage();
        List<Storehouseroom> list = storehouseroomService.selectStorehouseroomList(storehouseroom);
        return getDataTable(list);
    }

    /**
     * 导出库间管理列表
     */
    @PreAuthorize("@ss.hasPermi('store:storehouseroom:export')")
    @Log(title = "库间管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Storehouseroom storehouseroom)
    {
        List<Storehouseroom> list = storehouseroomService.selectStorehouseroomList(storehouseroom);
        ExcelUtil<Storehouseroom> util = new ExcelUtil<Storehouseroom>(Storehouseroom.class);
        return util.exportExcel(list, "storehouseroom");
    }

    /**
     * 获取库间管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('store:storehouseroom:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        Storehouseroom storehouseroom = storehouseroomService.selectStorehouseroomById(guid);
        Integer count = this.storehouseroomService.checkDeleteStore(storehouseroom);
        if(count > 0){
            storehouseroom.setWhinfoData(true);
        }else{
            storehouseroom.setWhinfoData(false);
        }
        return AjaxResult.success(storehouseroom);
    }

    /**
     * 新增库间管理
     */
    @PreAuthorize("@ss.hasPermi('store:storehouseroom:add')")
    @Log(title = "库间管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Storehouseroom storehouseroom)
    {
        return toAjax(storehouseroomService.insertStorehouseroom(storehouseroom));
    }

    /**
     * 修改库间管理
     */
    @PreAuthorize("@ss.hasPermi('store:storehouseroom:edit')")
    @Log(title = "库间管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Storehouseroom storehouseroom)
    {
        if(storehouseroom.isWhinfoData()){
            Storehouseroom store = this.storehouseroomService.selectStorehouseroomById(storehouseroom.getGuid());
            if(!storehouseroom.getShrtype().equals(store.getShrtype())){
                return AjaxResult.error(store.getName()+"有库存数据，不允许修改库间类型");
            }
        }
        return toAjax(storehouseroomService.updateStorehouseroom(storehouseroom));
    }

    /**
     * 删除库间管理
     */
    @PreAuthorize("@ss.hasPermi('store:storehouseroom:remove')")
    @Log(title = "库间管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        boolean flag = false;
        String name = null;
        for(String guid : guids){
            Storehouseroom storehouseroom = this.storehouseroomService.selectStorehouseroomById(guid);
            Storehouseroom s = new Storehouseroom();
            s.setGuid(guid);
            Integer count = this.storehouseroomService.checkDeleteStore(s);
            if(count > 0){
                name = storehouseroom.getName();
                flag = true;
                break;
            }
        }
        if(flag){
            return AjaxResult.error(name+"有库存数据，不允许删除");
        }
        return toAjax(storehouseroomService.deleteStorehouseroomByIds(guids));
    }

    @GetMapping("/findStorehouse")
    public AjaxResult findStorehouse(Storehouseroom storehouseroom)
    {
        List<Storehouseroom> list = storehouseroomService.selectStorehouseroomList(storehouseroom);
        return AjaxResult.success(list);
    }
}
